package com.qingjiang.projectschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingjiang.projectschedule.mapper.DepartmentMapper;
import com.qingjiang.projectschedule.pojo.Department;
import com.qingjiang.projectschedule.service.DepartmentService;
import com.qingjiang.projectschedule.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author jx
 * @description 针对表【department】的数据库操作Service实现
 * @createDate 2024-03-20 10:10:59
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
        implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Department getDeptById(Integer deptId) {
        return departmentMapper.selectById(deptId);
    }

    @Override
    public String getDeptNameById(Integer deptId) {
        return departmentMapper.selectById(deptId).getDeptName();
    }

    @Override
    public List<Department> getDeptList() {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Department::getIsAbandon,1).orderByAsc(Department::getDeptId);
        List<Department> departments = departmentMapper.selectList(queryWrapper);
        return departments;
    }

    @Override
    public Result getAllDeptList() {
        List<Department> departments = departmentMapper.selectList(null);
        return Result.ok(departments);
    }

    @Override
    public Integer getDeptIdByName(String name) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getDeptName, name);
        Department department = departmentMapper.selectOne(queryWrapper);
        if (department == null) {
            return -1;
        }
        return department.getDeptId();
    }

    @Override
    public Result updateDept(Department department) {
        if (department.getIsAbandon() == 1) department.setAbandonTime(LocalDate.now().toString());
        else department.setAbandonTime(null);
        LambdaUpdateWrapper<Department> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Department::getDeptId,department.getDeptId()).set(Department::getIsAbandon,department.getIsAbandon()).set(Department::getAbandonTime,department.getAbandonTime())
                .set(Department::getYellowWeight,department.getYellowWeight()).set(Department::getRedWeight,department.getRedWeight());
        int rows = departmentMapper.update(null,updateWrapper);
        if(rows==1) return Result.ok(null);
        return Result.error(null);
    }

    @Override
    public Result insertDept(Department department) {
        int rows = departmentMapper.insert(department);
        if (rows == 1) return Result.ok(null);
        return Result.error(null);
    }

    @Override
    public Result getDepartmentsByType(String type) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getDeptType, type);
        List<Department> departments = departmentMapper.selectList(queryWrapper);
        return Result.ok(departments);
    }

    @Override
    public List<Department> getDeptByID(List<Integer> deptIDs) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Department::getDeptId).ne(Department::getIsAbandon,1);
        if (deptIDs != null && deptIDs.size() > 0) {
            queryWrapper.in(Department::getDeptId, deptIDs);
        }
        List<Department> departments = departmentMapper.selectList(queryWrapper);

        return departments;
    }
}




